// sendRequest
const sendRequest = function (url, method, data) {
  const promise = new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        let code = res.statusCode.toString()
        if (code.startsWith('2')) {
          resolve(res.data)
        } else {
          showToast('数据异常')
          reject(res)
        }
      },
      fail() {
        showToast('内部服务器异常！')
      }
    })
  })
  return promise
}
// sendRequestFail
const sendRequestFail = function (url, method, data) {
  const promise = new Promise((resolve) => {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/json'
      },
      success(res){
        resolve(res.data)
      },
      fail() {
        showToast('内部服务器异常！')
      }
    })
  })
  return promise
}
// showToast
const showToast = function (text, time = 2000, icon = 'none') {
  wx.showToast({
    title: text,
    icon: icon,
    duration: time,
  })
}
// api 
const url = "https://api-oil.zhihuiyouzhan.com.cn/index.php/v1"
const api = {
  user: url + '/signin/user', //获取用户的token
  info: url + '/user/info', //管理用户个人的信息
  SendSms: url + '/sms/SendSms', //发送验证码
  phone: url + '/user/phone', //提交用户手机号
  orders: url + '/user/orders', //获取订单列表
  detail: url + '/order/oil/', //获取用户油品订单详情    /oil/:id
  prepare: url + '/order/oil/prepare', //获取预油品订单
  oil: url + '/order/oil', //生成油品订单
  pay: url + '/pay/order', //油品订单支付
  coupons: url + '/user/coupons', //获取优惠券列表
  nearby: url + '/merchant/nearby', //获取附近商家
  guns: url + '/merchant/', //获取商户油枪   /merchant/:id/guns 
  userinfo: url + '/user/info', //个人信息,
  saving: url + '/card/saving', //充值
  paywords: url + '/payworks/order', //会员卡支付
  password: url + '/card/password', //密码修改
  reset: url + '/card/password/reset' //密码重置
}
//token
const token = null
//scrollHeight
const scrollHeight = function (height = 0) {
  let scrollHeight = 0
  wx.getSystemInfo({
    success: (res) => {
      scrollHeight = res.windowHeight - res.windowWidth / 750 * height
    }
  });
  return scrollHeight
}
//codeTepmpale
const codeTepmpale={
  login:10000,
  reset:10001
}


module.exports = {
  sendRequest,
  sendRequestFail,  
  showToast,
  api,
  token,
  scrollHeight,
  codeTepmpale
}